package org.karn.example.content_provider;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.util.Base64;


public class ContentFetcher {

    Cursor mCursor;
    DBManager dbm;
    
    ContentFetcher(Cursor cursor, Context context, DBManager dbm)
    {
        mCursor  = cursor;
        this.dbm = dbm;
    }

    public void /*ArrayList<bookmarItem>*/ getContent()
    {
        int count = mCursor.getCount();
        int size  = mCursor.getColumnCount();
        ArrayList<bookmarItem> bookmarkList = new ArrayList<bookmarItem>();
        
        for(int i = 0; i < count; i++)
        {
            String[] colNames = mCursor.getColumnNames();
            mCursor.moveToPosition(i);
            bookmarItem item = new bookmarItem();
            
            for(int j = 0; j < size; j++)
            {
                int colIndex = mCursor.getColumnIndex(colNames[j]);
                switch(colIndex)
                {
                    case 0:
                        //adding URL
                        item.url = mCursor.getString(0);
                        break;
                    case 1:
                        //adding favicon
                        byte[] byteArray = mCursor.getBlob(1);
                        if(byteArray != null)
                        {
                            String base64string = Base64.encodeToString(byteArray, Base64.NO_WRAP);
                            item.favicon = base64string;
                        }
                        else
                        {
                            item.favicon = "-";
                        }
                        break;
                    case 2:
                        //adding title
                        item.title = mCursor.getString(2);
                        break;
                    case 3:
                        //adding date of creation
                        item.created = mCursor.getString(3);
                        break;
                }
            }
            dbm.addRow(item);
            //bookmarkList.add(item);
        }
        //return bookmarkList;
        mCursor.close();
    }

    private void fillDataBase(DBManager dbm)
    {
        //ArrayList<bookmarItem> rows = getContent();
        //dbm.updateTable(rows);
    }
    
}
